On Error Resume Next

fromemail = "ch00sm33@mt.com"
toemail = "datacenter.support@mt.com"
'toemail = "craig.david@mt.com"


'--------------------------<Functions>--------------------------

Function sendMail(strTo, strFrom, strSubject, strBody)
	'sendet Mail an strTo, von strFrom, Subject strSubject, Body strBody 
	Set objEmail = CreateObject ("CDO.Message")
	objEmail.From = strFrom
	objEmail.To = strTo
	objEmail.Subject = strSubject 
	objEmail.Textbody = strBody
	objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
	objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtpout.eu.mt.mtnet"  
	objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
	objEmail.Configuration.Fields.Update
	objEmail.Send
End Function 

Function getTime(StrLine)
	ASTime = Split(trim(StrLine))
	sDay = ASTime(0)
	sMonth = ASTime(1)
	sDate = ASTime(2)
	sTime = ASTime(3)
	sYear = ASTime(4)
	getTime = sDate + "-" + sMonth + "-" + right(sYear,2) + " " + trim(sTime)
end Function

Function convertTime(seconds)
   ConvSec = seconds Mod 60
   If Len(ConvSec) = 1 Then
         ConvSec = "0" & ConvSec
   End If
   ConvMin = (seconds Mod 3600) \ 60
   If Len(ConvMin) = 1 Then
         ConvMin = "0" & ConvMin
   End If
   ConvHour =  seconds \ 3600
   If Len(ConvHour) = 1 Then
         ConvHour = "0" & ConvHour
   End If
   convertTime = ConvHour & ":" & ConvMin & ":" & ConvSec
End Function

function units(sunit)
	if sunit = "KB" then
	  units = 1024
	  exit function
	end if
	if sunit = "MB" then
	  units = 1048576
	  exit function
	end if
	if sunit = "GB" then
	  units = 1073741824
	  exit function
	end if
	if sunit = "TB" then
	  units = 1099511627776
	  exit function
	end if
end function

Function ConvertSize(Size) 
Do While InStr(Size,",") 'Remove commas from size 
    CommaLocate = InStr(Size,",") 
    Size = Mid(Size,1,CommaLocate - 1) & _ 
        Mid(Size,CommaLocate + 1,Len(Size) - CommaLocate) 
Loop

Suffix = " Bytes" 
If Size >= 1024 Then suffix = " KB" 
If Size >= 1048576 Then suffix = " MB" 
If Size >= 1073741824 Then suffix = " GB" 
If Size >= 1099511627776 Then suffix = " TB" 
 
Select Case Suffix 
    Case " KB" Size = Round(Size / 1024, 1) 
    Case " MB" Size = Round(Size / 1048576, 1) 
    Case " GB" Size = Round(Size / 1073741824, 1) 
    Case " TB" Size = Round(Size / 1099511627776, 1) 
End Select

	ConvertSize = Size & Suffix 
End Function

Function getSize(sline)
	On Error Resume Next
	s = trim(split(sline,",")(1))
	size = split(s)(0)
	size = units(split(s)(1)) * size
	getSize = size
End Function

Function ShowError(strMessage)
    Error_code = strMessage + vbcrlf
    Error_code = Error_code + Err.Number & " Srce: " & Err.Source & " Desc: " &  Err.Description + vbcrlf
    Err.Clear
	ShowError = Error_code
End Function

Function stripSubject(subject)

	temp = trim(Split(subject,":")(1))
	myArray = split(temp,",")

	for i = 0 to ubound(myarray)
		if InStr(myarray(i),"Total") then
			tot = split(myarray(i))
			add2Array("Total" + vbtab + vbtab + vbtab + "= " + tot(2))
		end if	
		if InStr(myarray(i),"Disabled") then
			dis = split(myarray(i))
			add2Array("Disabled" + vbtab + vbtab + "= " + dis(1))
		end if	
		if InStr(myarray(i),"Failed") then
			fail = split(myarray(i))
			fails = "Failed" + vbtab + fail(1) 
			add2Array("Failed" + vbtab + vbtab + vbtab + "= " + fail(1))
		end if	
		if InStr(myarray(i),"Succeeded") then
			succ = split(myarray(i))
			add2Array("Succeeded" + vbtab + vbtab + "= " + succ(1))
		end if	
	next
	stripSubject = clientCntSubject
End Function	
	
sub add2Array(e)
	arraycnt = arraycnt + 1
	clientCntSubject(arraycnt) = e
End sub

'--------------------------<Functions>--------------------------

Dim fso, strm

sSize = 0
savegroup_cnt = 0
sizeLine = ""
Dim clientCntSubject(6)
arraycnt = 0

Set fso = CreateObject("Scripting.FileSystemObject")
Set sout = WScript.StdOut

Do Until WScript.StdIn.AtEndOfStream
    strLine = WScript.StdIn.ReadLine
    if Left(strLine,20) = "NetWorker savegroup:" then
		subject = strLine
	end If
	if Left(strLine,11) = "Start time:" then
	'Start time:   Fri Apr 12 23:17:33 2013
		sLineTime = right(strLine,Len(strLine) - 11)
		StartLine = strLine
		SStartTime = "Start time"
	end if
	if Left(strLine,13) = "Restart time:" then
	'Start time:   Fri Apr 12 23:17:33 2013
		sLineTime = right(strLine,Len(strLine) - 13)
		StartLine = strLine
		SStartTime = "Restart time"
	end if
	if Left(strLine,9) = "End time:" then
		eLineTime = right(strLine,Len(strLine) - 9)
		endLine = strLine
	'End time:     Fri Apr 12 23:17:59 2013
	end if
	if Left(strLine,7) = "Failed:" then
		sFailed = strLine
		sMessage_Failed = right(strLine,Len(strLine) - 7)
	end if
	if Left(strLine,9) = "Disabled:" then
		sDisabled = strLine
		sMessage_sDisabled = right(strLine,Len(strLine) - 9)
	end if
	if Left(strLine,10) = "Succeeded:" then
		sSucceeded = strLine
		sMessage_succeeded = right(strLine,Len(strLine) - 10)
	end if
	if Left(strLine,2) = "  " then
		sSize = sSize + getSize(strLine)
		sizeLine = SizeLine + strLine + vbcrlf
		savegroup_cnt = savegroup_cnt + 1
	end if
	body = body + strLine + vbCrLf
Loop

'NetWorker savegroup: (notice) D_DB_SQLGR completed, Total 75 client(s), 4 Clients Disabled, 71 Succeeded.

savegroup = trim(Split(subject,":")(1))
tots = savegroup
savegroup = Split(savegroup)(1)

totalClients = split(tots,",")(1)
ClientDisable = split(tots,",")(2)
Succeeded = split(tots,",")(3)

ss = getTime(sLineTime)
es = getTime(eLineTime)

sec = dateDiff("s",ss,es)
sMessage_savegroup = savegroup
Savegroup = "Save Group" + vbtab + vbTab + "= " + savegroup
Duration = "Duration" + vbtab + vbTab +  "= " + cstr(converttime(sec)) + "   (HH:MM:SS)"
SSize = "Size" + vbTab + vbTab + vbTab + "= " + ConvertSize(Ssize)
sSavegroup_cnt = "Savegroup count" + vbTab +  "= " + Cstr(savegroup_cnt)
StartTime = SStartTime + vbtab + vbTab + "= " + trim(split(trim(startline),": ")(1))
EndTime = "End Time" + vbtab + vbTab + "= " + trim(split(trim(endline),": ")(1))

Summary = Savegroup + vbCrLF
Summary = Summary + duration + vbCrLf 
Summary = Summary + SSize + vbCrLf 
Summary = Summary + sSavegroup_cnt + vbCrLf 
Summary = Summary + Starttime + vbCrLf
Summary = Summary + endtime + vbCrLf
Summary = Summary + vbCrLf

ClientCnt = stripsubject(subject)

for i = 0 to arraycnt
	Summary = Summary + ClientCnt(i) + vbcrlf
next

'Summary = summary + vbcrlf

Summary = Summary + sSucceeded + vbCrLf
Summary = Summary + sFailed + vbCrLf
Summary = Summary + sDisabled + vbCrLf

'Debug_lines = "<---Debug---->" + vbcrlf + sizeline + "<---Debug---->"

'Summary = summary + debug_lines + vbcrlf


body = Summary + vbCrLF + "<-------- Original Log Start ------->" + vbCrLF + vbCrLF + body + vbCrLF + "<-------- Original Log End ------->"

If Err.Number <> 0 Then 
   body = body + vbcrlf + ShowError("It failed")
end if

'sout.write(body)
sendMail toemail, fromemail, subject, body

'sout.write("Tot" + totalClients) + vbCrLf
'sout.write("Disabled" + ClientDisable) + vbCrLf
'sout.write("Succeeded" + Succeeded) + vbCrLf

sout.write(Cstr(savegroup_cnt))
sout.write(Cstr(sFailed))
sout.write(Cstr(sMessage_succeeded))

Set objShell = WScript.CreateObject("WScript.Shell")

Return = objShell.Run("e:\scripts\java\savegroup.cmd NW_savegroup_count " + Cstr(savegroup_cnt), 0, true)
if (sMessage_Failed <> "") then
	sz = Split(sMessage_Failed ,",")
	for i = 0 to ubound(sz)
		Return = objShell.Run("e:\scripts\java\savegroup.cmd NW_Client_failure " + trim(sz(i)), 0, true)
	next
end if

if (sMessage_succeeded <> "") then
	sz = Split(sMessage_succeeded ,",")
	for i = 0 to ubound(sz)
		Return = objShell.Run("e:\scripts\java\savegroup.cmd NW_Client_good " + trim(sz(i)), 0, true)
	next
end if

if (sMessage_sDisabled <> "") then
	sz = Split(sMessage_sDisabled ,",")
	for i = 0 to ubound(sz)
		Return = objShell.Run("e:\scripts\java\savegroup.cmd NW_Client_disabled " + trim(sz(i)), 0, true)
	next
end if

sz = Split(subject,")")(0)
sz = Split(sz,"(")(1)
sout.write(sz)

Return = objShell.Run("e:\scripts\java\savegroup.cmd NW_savegroup_" + trim(sz) + " " + sMessage_savegroup, 0, true)






WScript.Quit(0)

' To run this program in test.
'
' type sg.txt | cscript sg_email.vbs
'
'
